﻿using rmTest.Common;
using rmTest.Common.ConstUtil;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ProgramTest
{
    public class TaskShow
    {
        public static void Show()
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            bool bGameIsOver = false;
            CancellationTokenSource cts = new CancellationTokenSource();//bool值  
            Task.Run(() =>
            {
                int i = 0;
                while (i != DateTime.Now.Year)
                {
                    if (cts.IsCancellationRequested || bGameIsOver)
                    {
                        return;
                    }
                    i = new Random().Next(0, 10000);
                    Thread.Sleep(50);
                }
                cts.Cancel();
                PrintLog("天降雷霆灭世，天龙八部的故事就此结束....", true);
            });

            bool bFirstIsOver = false;
            List<Task> tasks = new List<Task>();
            foreach (PersonAction personAction in InitData.PersonActions)
            {
                if (!cts.IsCancellationRequested)
                {
                    PrintLog($"我是：{personAction.Name}；", personAction.Color, true);
                }
                Action<Object> action = (o) =>
                {
                    foreach (string act in personAction.Actions)
                    {
                        PrintLog($"{personAction.Name}现在正在发生的是：{act}；", personAction.Color);
                        if (!bFirstIsOver)
                        {
                            lock (ConstArgs.ObjectLock)
                            {
                                if (!bFirstIsOver)
                                {
                                    PrintLog("天龙八部就此拉开序幕。。。。", true);
                                    bFirstIsOver = true;
                                }
                            }
                        }
                    };
                };
                if (!cts.IsCancellationRequested)
                {
                    Task task = new Task(action, personAction.Name, cts.Token);//传入信号量参数
                    task.Start();
                    tasks.Add(task);
                }
            }


            tasks.Add(Task.WhenAny(tasks.ToArray()).ContinueWith(t =>
            {
                if (!cts.IsCancellationRequested)
                {
                    PrintLog($"{t.Result.AsyncState}已经做好准备啦。。。。", true);
                }
            }));
            tasks.Add(Task.WhenAll(tasks.ToArray()).ContinueWith(t =>
            {
                if (!cts.IsCancellationRequested)
                {
                    PrintLog("中原群雄大战辽兵，忠义两难一死谢天", true);
                }
            }));

            Task.WaitAll(tasks.ToArray());
            stopwatch.Stop();
            PrintLog($"整个故事用时：{ stopwatch.ElapsedMilliseconds}", true);
            bGameIsOver = true;
            Console.ReadKey();
        }

        private static void PrintLog(string s, ConsoleColor consoleColor, bool b = false)
        {
            ConsoleUtil.Output(s, consoleColor, b);
            LogUtil.Instance.WriteLog(s);
        }

        private static void PrintLog(string s, bool b = false)
        {
            ConsoleUtil.Output(s, ConsoleColor.White, b);
            LogUtil.Instance.WriteLog(s);
        }
    }
}
